import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_business_core/business_customer/ful_components/business_customer_details_edit_label_tab_form.dart';
import 'package:x3_component_core/x3_component_core.dart';

class BusinessCustomerDetailsEditLabelTab extends StatefulWidget {
  BusinessCustomerDetailsEditLabelTab({Key key, @required this.response, @required this.vipId}) : super(key: key);

  final BaseHttpResponseModel response;

  final String vipId;

  @override
  _BusinessCustomerDetailsEditLabelTabState createState() => _BusinessCustomerDetailsEditLabelTabState();
}

class _BusinessCustomerDetailsEditLabelTabState extends State<BusinessCustomerDetailsEditLabelTab> with AutomaticKeepAliveClientMixin implements BaseCatalogResponse {
  Map<String, dynamic> _dataSource;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return BaseScreenRefreshComponent(
      request: new BaseScreenRequestModel(widget.response.inputApi, params: {'vipId': widget.vipId}),
      builder: _buildScaffoldWidget,
    );
  }

  Widget _buildScaffoldWidget(BaseHttpResultModel requestResult) {
    return BaseScreenRefreshComponent(
      request: BaseScreenRequestModel(widget.response.webUrl, method: BaseHttpMethodEnum.GET, params: {'vipid': widget.vipId}),
      builder: (BaseHttpResultModel vipResult) => _buildContent(vipResult, requestResult),
    );
  }

  Widget _buildContent(BaseHttpResultModel vipResult, BaseHttpResultModel requestResult) {
    this._initDataSource(vipResult);
    return ListView(
      padding: EdgeInsets.only(bottom: 20.0),
      children: BaseCatalogController.catalogResponseComponents(requestResult, this.catalog()),
    );
  }

  @override
  Map<String, Widget Function(BaseHttpResponseModel response, dynamic dataSource)> catalog() {
    return {
      'MainBusinessContent': (BaseHttpResponseModel response, dynamic dataSource) => BusinessCustomerDetailsEditLabelTabForm(response: response, dataSource: dataSource, formDataSource: _dataSource),
    };
  }

  /// 获取默认参数
  void _initDataSource(BaseHttpResultModel vipResult) {
    if (this._dataSource != null) return;
    dynamic arguments = BaseRouterUtil.getArguments(context);
    String vipId = BaseUtils.getKeyData(arguments, 'vipId').toString();
    List<dynamic> datas = vipResult.data ?? [];
    Map<String, dynamic> dataSource = {'vipId': vipId};

    datas.forEach((data) {
      String propertyId = BaseUtils.getKeyData(data, 'propertyId').toString();
      String value = BaseUtils.getKeyData(data, 'value').toString();
      String valueText = BaseUtils.getKeyData(data, 'valueText').toString();
      bool isMultiple = value.contains(',');

      /// 多选
      if (isMultiple == true) {
        List _val = value.split(',');
        List _valText = valueText.split(',');
        List<Map<String, dynamic>> _data = [];
        for (int i = 0; i < _val.length; i++) {
          _data.add({'key': _val[i], 'value': _valText[i]});
        }
        dataSource.addAll({propertyId: _data});
      } else {
        // 单选
        dataSource.addAll({
          propertyId: {'key': value, 'value': valueText}
        });
      }
    });
    this._dataSource = dataSource;
  }

  @override
  bool get wantKeepAlive => true;
}
